<?php
namespace Lmkj\Controller;
use Think\Controller;

//use Lmsj\Common\Model;

class LoginController extends  Controller {

	private $user;
	/**
	 * 自动运行函数
	 */
	public function __construct() {
		parent::__construct();
		$this -> user = D('Member');
	}

	/**
	 * 显示登录首页
	 */
	public function index() {
		$this -> display();
	}
	/**
	 * ajax验证用户名和密码
	 */
	public function ajaxUser() {
		if(IS_AJAX){
			$code = I('post.code');
			$id = '';
			//检测验证码
			$verify = new \Think\Verify();
			if(!$verify->check($code, $id)){
				$this->ajaxReturn(2);
			}
			//验证用户名和密码
			$data = $this->user->getOneData();
			if($data){
				$this->user->updateSuccessData($data['member_id']);				
				//存下session		
				$userName = new \stdClass();
				$userName->uid =  $data['member_id'];
				$userName->user = $data['username'];
				$userName->role = $data['role'];
				$userName->time = date('Y-m-d H:i:s',time());
				session('userName',$userName);
				$this->ajaxReturn(1);
			}else{
				$this->ajaxReturn(0);
			}
		}
	}

	/**
	 * 退出登录
	 */
	public function loginOut() {
		session('userName', null);
		header("Content-type:text/html;charset=utf-8;");
		redirect(U('Lmkj/Login/index'), 1, '正在退出登录,请稍后...');
	}

	/**
	 * 验证码
	 */
	Public function verify() {
		$config = array(
			'fontSize' => 50, // 验证码字体大小
			'length' => 4, // 验证码位数
			'useNoise' => false, // 关闭验证码杂点
		);
		$Verify = new \Think\Verify($config);
		$Verify -> entry();
	}

}
